package game;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JInternalFrame;
import main.Runner;
import main.CSVReader;

public class Game extends JInternalFrame{
    /** String to represent ID of test subject*/
    public static String playerId="DEFAULT";

    /** flag to indicate whether the game board is free*/
    public static boolean free;

    /** values for height and width of games*/
    public static int WIDTH=800,HEIGHT=600;

    /** Maximum time limit in seconds for each game*/
    int secs=10;

    /** Debug mode which shortens the time period for instructions. */
    public static boolean debugMode = false;

    /** Name of the current running game*/
    public static String currGame="Ultimatum";//default value

    /** Keeps track of total money won at that stage*/
    public static double totalMoneyWon = 0.0 ;

    /** HashMap to store info of whether a game has already played in the session before*/
    public static Map<String,Boolean> played = new HashMap<String,Boolean>();

    /** Runner object*/
    public static Runner runner;

    /** Boolean to indicate the first instance of the class*/
    static boolean first=true;

	// Information needed for log files.
	/** The parameters as they were given to us. */
	String params;

	/** The current number of times the subject has played this game. */
	int repNum;

	/** The total number of times this game will be placed by the subject. */
	int repTot;

    public Game() {
        super("",
              false, //resizable
              false, //closable
              false, //maximizable
              false);//iconifiable
        javax.swing.plaf.InternalFrameUI ui = this.getUI();
        ((javax.swing.plaf.basic.BasicInternalFrameUI)ui).setNorthPane(null);

//        runner = run;        
    }
    /**Function to write an entry in the log file*/
    public void writeLog(String s) {
        try {
            FileOutputStream fout = new FileOutputStream("logs/Log.txt", true);
            PrintStream p = new PrintStream(fout);
            p.println(s);
        }
        catch(Exception ex) {
            ex.printStackTrace();
            logError(ex);
        }
    }
    /*Function to write a log for a particular game*/
    public void writeGameLog(String s) {
        try {
            FileOutputStream fout = new FileOutputStream("logs/"+currGame+"_log.csv", true);
            PrintStream p = new PrintStream(fout);
            p.println(s);
        }
        catch(Exception ex) {
            ex.printStackTrace();
            logError(ex);
        }
    }

    /*Function to log error from an exception*/
    public void logError(Exception ex) {
        try {
            FileOutputStream fout = new FileOutputStream("err.txt",false);
            PrintStream p = new PrintStream(fout);
            ex.printStackTrace(p);
        } catch(Exception ex1) {
            ex1.printStackTrace();
        }
    }

    /*Function to read data from a file. Returns a BufferedReader for that file*/
    public BufferedReader readGameData() {
        FileReader fin = null;
        BufferedReader bin = null ;
        try  {
            fin = new FileReader("data/"+currGame+"_data.csv");
            bin = new BufferedReader(fin);
        }
        catch(Exception e) {
            e.printStackTrace();
            logError(e);
        }
        return bin;
    }

    /* Function that reads CSV files. */
    public ArrayList<ArrayList<String>> readCSVFile (String file) {
        try {
            CSVReader reader = new CSVReader (new FileReader(new File (file)));
            return (ArrayList<ArrayList<String>>) reader.readAll();
        }
        catch (Exception E) {
            System.err.println ("[ERROR]: couldn't read CSV file " + file);
            return null;
        }
    }

    /*Function to read data from a file. Returns a BufferedReader for that file*/
    public BufferedReader readFile(String s) {
        FileReader fin = null;
        BufferedReader bin = null ;
        try  {
            fin = new FileReader(s);
            bin = new BufferedReader(fin);
        }
        catch(Exception e) {
            e.printStackTrace();
            logError(e);
        }
        return bin;
    }

    /*Function to write an entry into the specified file*/
    public void writeFile(String fname, String s) {
        try {
            FileOutputStream fout = new FileOutputStream(fname, true);
            PrintStream p = new PrintStream(fout);
            p.println(s);
        }
        catch(Exception ex) {
            ex.printStackTrace();
            logError(ex);
        }
    }
}
